package com.woniuxy.joylifemarket.service.impl;

import com.woniuxy.common.entity.CenterUser;
import com.woniuxy.common.entity.MarketAppraise;
import com.woniuxy.common.entity.MarketCommodity;
import com.woniuxy.common.mapper.CenterUserMapper;
import com.woniuxy.common.mapper.MarketAppraiseMapper;
import com.woniuxy.common.mapper.MarketCommodityMapper;
import com.woniuxy.common.mapper.MarketOrderMapper;
import com.woniuxy.common.utils.KeyUtil;
import com.woniuxy.joylifemarket.service.MarketAppraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MarketAppraiseServiceImpl implements MarketAppraiseService {
    @Autowired
    MarketCommodityMapper marketCommodityMapper;
    @Autowired
    MarketAppraiseMapper marketAppraiseMapper;
    @Autowired
    CenterUserMapper centerUserMapper;
    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Override
    public List<MarketAppraise> findAllAppraiseByUserId(String userid) {
        List<MarketAppraise> marketAppraises = marketAppraiseMapper.selectByUserId(userid);
        for (MarketAppraise marketAppraise:marketAppraises) {
            CenterUser sendUser = centerUserMapper.selectByPrimaryKey(marketAppraise.getSenduserId());
            marketAppraise.setSenduser(sendUser);
            MarketCommodity marketCommodity = marketCommodityMapper.selectByPrimaryKey(marketAppraise.getCommodityId());
            marketAppraise.setCommodity(marketCommodity);
        }
        return marketAppraises;
    }

    @Transactional
    @Override
    public int insertAppraise(MarketAppraise marketAppraise,String orderId) {
        marketAppraise.setId(KeyUtil.generateKey());
        marketAppraise.setDate(new Date());
        int insert = marketAppraiseMapper.insert(marketAppraise);
        if(insert!=0){
            marketOrderMapper.updateAppraiseStatusById(orderId);
        }
        return insert;
    }
}
